﻿using System;
using System.Reflection;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Shapes;

namespace SHomeWorkshop.LunarMind
{
    /// <summary>
    /// Interaction logic for ColorPicker.xaml
    /// </summary>
    public partial class ColorPicker : UserControl
    {
        public ColorPicker()
        {
            InitializeComponent();
        }

        public enum Destination { Foreground, Background, Border, Other }

        public Destination DestAction { get; set; }

        private void Rectangle_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            Brush selectedColor = (sender as Rectangle).Fill;
            OnColorPicked(sender,
                new ColorPickedEventArgs() { PickedColor = selectedColor });
        }

        public event EventHandler<ColorPickedEventArgs> ColorPicked;

        public event EventHandler<EventArgs> Closed;

        protected void OnClosed(object sender, EventArgs e)
        {
            colorName.Text = "..右击色块复制名称..";
            if (Closed != null)
            {
                Closed(this, e);
            }
        }

        protected void OnColorPicked(object sender, ColorPickedEventArgs e)
        {
            if (ColorPicked != null)
            {
                ColorPicked(this, e);//注意，这里不用“sender”！！！
            }
        }

        public static Brush GetBrushByName(string brushName)
        {
            Type type = typeof(Brushes);
            PropertyInfo[] properties = type.GetProperties();
            foreach (PropertyInfo pi in properties)
            {
                if (pi.Name == brushName)
                {
                    return pi.GetValue(null, null) as Brush;
                }
            }
            return null;
        }

        public static string GetBrushName(Brush brush)
        {
            if (brush == null) return "UnKnow";

            PropertyInfo[] properties = typeof(Brushes).GetProperties();
            foreach (PropertyInfo pi in properties)
            {
                if (brush == pi.GetValue(null, null) as Brush)
                {
                    return pi.Name;
                }
            }

            return "UnKnow";
        }

        private void Image_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            OnColorPicked(sender,
                new ColorPickedEventArgs() { PickedColor = Brushes.Transparent });
        }

        private void Rectangle_MouseEnter(object sender, MouseEventArgs e)
        {
            colorName.Text = ColorPicker.GetBrushName((sender as Rectangle).Fill);
        }

        private void Image_MouseEnter(object sender, MouseEventArgs e)
        {
            colorName.Text = ColorPicker.GetBrushName(Brushes.Transparent);
        }

        private void closeImage_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            OnClosed(this, e);
        }

        private void Image_MouseRightButtonDown(object sender, MouseButtonEventArgs e)
        {
            Clipboard.SetData(DataFormats.UnicodeText, ColorPicker.GetBrushName(Brushes.Transparent));
            OnClosed(this, e);
        }

        private void Rectangle_MouseRightButtonDown(object sender, MouseButtonEventArgs e)
        {
            Clipboard.SetData(DataFormats.UnicodeText, ColorPicker.GetBrushName((sender as Rectangle).Fill));
            OnClosed(this, e);
        }

        private void WrapPanel_MouseEnter(object sender, MouseEventArgs e)
        {
            colorName.Text = "..右击色块复制名称..";
        }

        private void DockPanel_MouseEnter(object sender, MouseEventArgs e)
        {
            colorName.Text = "..右击色块复制名称..";
        }
    }

    public class ColorPickedEventArgs : EventArgs
    {
        public Brush PickedColor { get; set; }
    }
}
